package com.kd.crawler.crawl;

import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.kd.crawler.common.jms.ActiveMQProducer;
import com.kd.crawler.common.utils.HttpClientUtils;
import com.kd.crawler.common.utils.PropertiesUtils;
import com.kd.crawler.dao.crawler.CrawlerEntryDao;
import com.kd.crawler.entity.CrawlerEntry;
import com.kd.crawler.http.HttpResponse;

public class Spider implements Runnable {

	private static final Logger logger = Logger.getLogger(Spider.class);
	private CrawlerEntry cu = null;
	private SpiderListener listener = null;
	
//	private HttpWebClient httpUtils=new HttpWebClient();
	
	public Spider(SpiderListener spiderListener) {
		this.listener = spiderListener;
	}

	public void run() {
		try{
			doCrawl();
		}catch(Exception e){
			logger.error("爬虫报错:"+cu.getUrl(),e);
		}
		finally{
			listener.onFinish(this);
		}		
	}
	
	private void doCrawl(){
		Properties properties = PropertiesUtils.getProperties("activeMQ.properties");
		if(null==cu || null==cu.getUrl()) {
			return ;
		}
//		httpUtils.crawl(cu);	
		HttpResponse response = HttpClientUtils.httpGetRequest(cu.getUrl());
		cu.setHtml(response.getHtml());
        cu.setStatusCode(response.getStatus());
        cu.setCrawlTime(response.getCrawlTime());
        CrawlerEntryDao.getInstance().insert(cu);
        /*
	    if(200 == cu.getStatusCode()) {
	    	logger.info(cu.getUrl() + " --> " + cu.getStatusCode());
//	    	ActiveMQProducer.sendQueueMessage(properties.getProperty("QUEUE_CRAWL_HTML_MESSAGE_OLD"), JSON.toJSONString(cu));
//	    	ActiveMQProducer.sendTopicMessage(properties.getProperty("QUEUE_CRAWL_HTML_MESSAGE"), JSON.toJSONString(cu));
//	    	ActiveMQProducer.sendQueueMessage(properties.getProperty("QUEUE_CRAWL_HTML_MESSAGE"), JSON.toJSONString(cu));
//	    	CrawlerEntryDao.getInstance().update(false, cu.getEntryId());
		}else if(403 == cu.getStatusCode()){
			logger.info(cu.getUrl() + " --> " + cu.getStatusCode());
			cu.setCrawlTime(null);
			cu.setHtml(null);
			cu.setStatusCode(0);
//			ActiveMQProducer.sendQueueMessage(properties.getProperty("QUEUE_CRAWL_ENTRY_MESSAGE"), JSON.toJSONString(cu));
		}else {
			if(!cu.isDetailed()){
				logger.error(" 入口地址访失败 " +JSON.toJSONString(cu));
			}else{
				logger.error(cu.getUrl() + " --> " + cu.getStatusCode());
			}
		} 
		*/
	}

	public void setCrawlerUrl(CrawlerEntry cu) {
		this.cu = cu;		
	}
	public CrawlerEntry getCrawlerUrl() {
		return cu;
	}
	public boolean match(String s, String pattern) {  
        Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE + Pattern.UNICODE_CASE);  
        Matcher matcher = p.matcher(s);  
        if (matcher.find()) {  
            return true;  
        } else {  
            return false;  
        }  
    } 
	
	public interface SpiderListener {

		public void onFinish(Spider spider);
		
	}
}
